<template>
  <div v-if="network">
    <Back :title=" $t('NetContent_networkDetail')"></Back>
    <div class="from">
      <div class="title">
       {{ $t('NetContent_networkDetailInfo')}}
      </div>
      <ul>
        <li>
          <div class="head">ID</div>
          <div class="body">
            {{ network.Id }}
          </div>
        </li>
        <li>
          <div class="head"> {{ $t('NetContent_name')}}</div>
          <div class="body">
            {{ network.Name }}
          </div>
        </li>
        <li>
          <div class="head">{{ $t('NetContent_Driver')}}</div>
          <div class="body">
            {{ network.Driver }}
          </div>
        </li>
        <li>
          <div class="head">Scope</div>
          <div class="body">
            {{ network.Scope }}
          </div>
        </li>
        <li>
          <div class="head"> {{$t('NetContent_append')}}</div>
          <div class="body">
            {{ network.Attachable }}
          </div>
        </li>
        <li>
          <div class="head">{{$t('NetContent_internalNetwork')}} </div>
          <div class="body">
            {{ network.Internal }}
          </div>
        </li>
        <li>
          <div class="head"> {{$t('NetContent_subNetwork')}}</div>
          <div class="body">
            {{ network.IPAM.Config.length > 0 ? network.IPAM.Config[0].Subnet : '...' }}
          </div>
        </li>
        <li>
          <div class="head"> {{$t('NetContent_gateway')}}</div>
          <div class="body">
            {{ network.IPAM.Config.length > 0 ? network.IPAM.Config[0].Gateway : '...' }}
          </div>
        </li>
      </ul>
      <el-button type="danger" v-if="(network.Name !== 'bridge') && (network.Name !== 'host') && (network.Name !== 'none')" icon="el-icon-delete" @click="volumeDel" style="margin: 20px 0px 0px 10px">删除网络</el-button>
    </div>
    <div class="from" v-if="JSON.stringify(network.Containers) !== '{}'">
      <div class="title">
         {{$t('NetContent_containerUsedNetwork')}}
      </div>
      <el-table
      :data="container"
      size="small"
      style="width: 100%">
        <el-table-column
          prop="Name"
          :label=" $t('NetContent_containerName') ">
        </el-table-column>
        <el-table-column
          :label="$t('NetContent_address')">
          <template slot-scope="scope">
            {{ scope.row.IPv4Address || '...' }}
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('NetContent_address') ">
          <template slot-scope="scope">
            {{ scope.row.IPv6Address || '...' }}
          </template>
        </el-table-column>
        <el-table-column
          :label="  $t('NetContent_operation')">
          <template slot-scope="scope">
            <el-button type="danger" size="mini" @click="delContainer(scope.row.Name)">断开连接</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
  <div v-else>
    <Back title=" $t('NetContent_networkDetail')  "></Back>
     {{   $t('NetContent_networkError')}}
  </div>
</template>

<script>
import Vue from 'vue'
import Back from '@/components/Back'
import post from '@/utils/post'
import { Table, TableColumn, Button } from 'element-ui'
Vue.use(Table)
Vue.use(TableColumn)
Vue.use(Button)

export default {
  data () {
    return {
      network: null,
      container: []
    }
  },
  mounted () {
    // 调用获取网络数据的函数
    this.fetchNet()
  },
  methods: {
    // 断开容器和网络的连接
    delContainer (id) {
      const data = {
        Container: id,
        Force: false
      }
      const docker = `curl -w %{http_code} --unix-socket /var/run/docker.sock -s -H Content-Type:application/json -d '${JSON.stringify(data)}' "http:/v1.40/networks/${this.network.Id}/disconnect"` // 将容器移出网络 -w %{http_code}显示状态码
      post({ docker }, res => {
        if (res >= 200 && res < 300) {
          this.$message({
            showClose: true,
            message: this.$t('NetContent_disconnectSuccess'),
            type: 'success'
          })
          this.fetchNet()
        } else {
          this.$message({
            showClose: true,
            message: this.$t('NetContent_networkError'),
            type: 'error'
          })
        }
      }, this)
    },
    // 删除卷
    volumeDel () {
      if (!confirm(this.$t('NetContent_confirmDeleteDataVolume')+"？")) {
        return
      }
      const docker = `curl -w %{http_code} --unix-socket /var/run/docker.sock -s -X DELETE "http:/v1.40/networks/${this.network.Id}"` // 删除数据卷
      post({ docker }, res => {
        if (res >= 200 && res < 300) {
          this.$message({
            showClose: true,
            message: this.$t('NetContent_dataVolumeDeleteSuccess'),
            type: 'success'
          })
          this.$router.go(-1)
        } else {
          this.$message({
            showClose: true,
            message: this.$t('NetContent_networkBadTryLater'),
            type: 'error'
          })
        }
      }, this)
    },
    // 获取网络信息
    fetchNet () {
      const docker = `curl --unix-socket /var/run/docker.sock -s "http:/v1.40/networks/${this.$route.params.id}"` // 查询卷列表
      post({ docker }, res => {
        if (!res.message) {
          this.network = res
          this.container = []
          for (let key in res.Containers) {
            this.container.push(res.Containers[key])
          }
        }
      }, this)
    }
  },
  components: {
    Back
  }
}
</script>

<style lang="scss" scoped>
.from{
  margin-top: 30px;
}
ul{
  li{
    display: flex;
    font-size: 14px;
    color: #333;
    .head{
      min-height: 50px;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-shrink: 0;
      width: 125px;
      margin-right: 30px;
      font-size: 16px;
      color: #067FB5;
      border-radius: 5px;
      background: #fff;
      border: 1px solid #067FB5;
    }
    &:nth-child(2n-1){
      background: #fff;
      .head:nth-child(2n-1){
        background: #F7FCFF;
      }
    }
    .body{
      padding: 5px 0px;
      font-size: 13px;
      width: 100%;
      overflow: hidden; //超出部分隐藏
      text-overflow: ellipsis; //超出部分显示...
      white-space: nowrap; //文本强制一行显示
      display: flex;
      flex-direction: column;
      justify-content: center;
    }
  }
}
</style>
